package com.offcn.controller;

import com.offcn.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class ThymeleafController {

    //演示1
    @RequestMapping("demo1")
    public String demo1(Model model){
        //创建一个数据
        String msg="Hello Thyleaf!";

        model.addAttribute("msg",msg);

        //跳转到视图,在跳转过程中触发调用Thymeleaf模板引擎，进行渲染,
        return "demo1";

    }


    //演示2：
    @RequestMapping("demo2")
    public String demo2(Model model){
        //创建一个用户对象
        User user = new User();
        user.setId(10001);
        user.setName("张三");
        user.setPrice(1888.89F);

        model.addAttribute("user",user);

        //创建一个Map
        Map map=new HashMap();
        map.put("id","1008");
        map.put("name","李四");
        map.put("address","张家庄");
        model.addAttribute("map",map);

        return "demo2";
    }

    //演示3：传递集合
    @RequestMapping("demo3")
    public String demo3(Model model){
        //创建一个list集合
        List list=new ArrayList();

        User user1 = new User();
        user1.setId(10001);
        user1.setName("张三");
        user1.setPrice(1888.89F);

        User user2 = new User();
        user2.setId(10002);
        user2.setName("王五");
        user2.setPrice(2888.89F);

        User user3 = new User();
        user3.setId(10003);
        user3.setName("赵柳");
        user3.setPrice(788.89F);

        User user4 = new User();
        user4.setId(10004);
        user4.setName("神器");
        user4.setPrice(88.89F);

        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        model.addAttribute("list",list);

        return "demo3";

    }

    //演示4：字符串拼接
    @RequestMapping("demo4")
    public String demo4(Model model){
        String name="张胜男";
        String msg="1999-10-1";

        String url="http://www.ujiuye.com";

        String picUrl="http://www.ujiuye.com/uploadfile/2021/0225/20210225064529134.png";

        model.addAttribute("name",name);
        model.addAttribute("msg",msg);
        model.addAttribute("url",url);
        model.addAttribute("pic",picUrl);
        return "demo4";
    }

    //演示5：条件判断、选择
    @RequestMapping("demo5")
    public String demo5(Model model){
        boolean is=false;

        model.addAttribute("is",is);

        model.addAttribute("menu","admin");
        model.addAttribute("manager","admin");

        return "demo5";
    }

    //演示6：
    @RequestMapping("demo6")
    public String demo6(Model model){
        model.addAttribute("now",new Date());

        model.addAttribute("num",0000.7891);
        model.addAttribute("msg","Thymeleaf是Web和独立环境的现代服务器端Java模板引擎，能够处理HTML，XML，JavaScript，CSS甚至纯文本。\r\n" +
                "Thymeleaf的主要目标是提供一种优雅和高度可维护的创建模板的方式。为了实现这一点，它建立在自然模板的概念上，将其逻辑注入到模板文件中，不会影响模板被用作设计原型。这改善了设计的沟通，弥补了设计和开发团队之间的差距。\r\n" +
                "Thymeleaf也从一开始就设计了Web标准 - 特别是HTML5 - 允许您创建完全验证的模板，如果这是您需要的\r\n");

        return "demo6";

    }

}
